//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        count: 1,
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 1000
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
            url: ''
        })
    },
    onLoad: function() {
        var that = this
            //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/index.do',
            success: function(res) {
                that.setData({
                    pictureList: res.data.pictureList,
                    couponList: res.data.couponList,
                    rqList: res.data.rqList,
                    jfList: res.data.jfList
                })
            }
        })
    },
    bindproductInforenqi: function(e) {
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/index.do',
            success: function(res) {
                that.setData({
                    rqList: res.data.rqList
                })
            }
        })
        wx.navigateTo({
            url: '../productDetail/productDetail?goodsId=' + e.currentTarget.id
        })
    },
    bindproductInfojf: function(e) {

        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/index.do',
            success: function(res) {
                that.setData({
                    jfList: res.data.jfList
                })
            }

        })
        wx.navigateTo({
            url: '../productDetail/productDetail?goodsId=' + e.currentTarget.id,
        })
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh()
    }, // 上拉加载回调接口
    onReachBottom: function() {
        var that = this
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/jfPage.do',
            data: {
                pageIndex: that.data.count
            },
            success: function(res) {
                var jfList1 = res.data.jfList;
                var jfList2 = that.data.jfList
                jfList2 = jfList2.concat(jfList1)
                that.setData({
                    jfList: jfList2
                })
            }
        })
        that.data.count++;
        // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    },
    // 领取优惠券
    coupon: function(e) {
        let couponId = e.currentTarget.dataset.id;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/userCoupon/add.do?',
            data: {
                openId: app.globalData.openid,
                couponId: couponId
            },
            success: function(res) {
                wx.showToast({
                    title: "领券成功"
                })
                wx.navigateTo({
                    url: '../personal/coupon/coupon'
                })
            }
        })
    }
})